﻿using System;
using System.Collections.Generic;
using ZX.EHR.Library;
using ZX.BLL.Extend;
using ZX.DBModule;
using ZX.SYS.Library;


namespace ZX.BLL
{
    /// <summary>
    /// 薪资中用到的函数
    /// </summary>
    public class fnSalary
    {

        /// <summary>
        /// 计算个人所得税函数
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public static double fnCalTax(object value)
        {
            try
            {
                double x = double.Parse(value.ToString());
                //先减去个税起征点
                DatPhraseDetail detail=new DatPhraseDetail();
                BaseBussnies detailBll = new BaseBussnies(detail);
                detail = detailBll.GetEntityByCondition("ClassID =(select ClassID from DatPhraseClass where ClassValue='BaseSalaryTax')") as DatPhraseDetail;
                if (detail != null)
                {
                    double startTax = 0;
                    try
                    {
                        startTax = double.Parse(detail.PhraseValue);
                    }
                    catch { }
                    x -= startTax;
                    if (x < 0) x = 0;
                }
                //计算
                salTaxRate tax = new salTaxRate();
                BaseBussnies bll = new BaseBussnies(tax);
                IList<BaseEntity> lists = bll.GetEntitys();
                foreach (BaseEntity entity in lists)
                {
                    tax = entity as salTaxRate;
                    double min = double.Parse(tax.MinValue.ToString());//最小值
                    double max = double.Parse(tax.MaxValue.ToString());//最大值
                    double kouchu = double.Parse(tax.Deduction.ToString());//扣除数
                    double taxRate = double.Parse(tax.TaxRate.ToString());//税率
                    if (x >= min && x < max)
                    {
                        double tempValue = System.Math.Round(x * taxRate - kouchu, 2);
                        return tempValue;
                    }
                }
                return 0;
            }
            catch(Exception ex)
            {
                try
                {
                    salSalarylErrorLog salaryErrLog = new salSalarylErrorLog();
                    BaseBussnies errBll = new BaseBussnies(salaryErrLog);
                    salaryErrLog.moduleName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;
                    salaryErrLog.className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName;
                    salaryErrLog.methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                    salaryErrLog.errMsg = ex.Message;
                    salaryErrLog.errDate = DateTime.Now;
                    errBll.Insert();
                }
                catch { }
                return -1;
            }
        }
    }
}
